<?php
class CodeSpace_Oauth_Server_Request_Token_Request extends CodeSpace_Oauth_Server_Request_Abstract
{

    public function __construct ($options = null)
    {
        parent::__construct($options);
    }

    public function createRequestToken ()
    {
        $requestToken = sha1(uniqid(rand(), true));
        $requestTokenSecret = sha1(uniqid(rand(), true));
        $this->_token = new Zend_Oauth_Token_Request();
        $this->_token->setToken($requestToken);
        $this->_token->setTokenSecret($requestTokenSecret);
    }

    public function buildControlSignature($config)
    {
        /*
         * Order for request token
         * 
         * oauth_consumer_key
         * oauth_nonce
         * oauth_timestamp
         * oauth_signature_method
         * oauth_version
         * oauth_callback
         * oauth_signature
         *
         */
        $params = array ();
        $params['oauth_consumer_key'] = $config->getConsumer()->getKey();
        $params['oauth_nonce'] = $config->getNonce();
        $params['oauth_timestamp'] = $config->getTimestamp();
        $params['oauth_signature_method'] = $config->getSignatureMethod();
        $params['oauth_version'] = $config->getVersion();
        $params['oauth_callback'] = $config->getCallbackUrl();
        
        foreach ($config->getUserParams() as $key => $value)
        {
            $params[$key] = $value;
        }
                
        $utility = new Zend_Oauth_Http_Utility;
        $sign = $utility->sign(
            $params,
            $config->getSignatureMethod(),
            $config->getConsumer()->getSecret(),
            null,
            'POST',
            'http://morpheus.gateway.mkb.com:41000/oauth/requesttoken'
        );
        
        return $sign;
    }
    
    public function process(CodeSpace_Oauth_Server $server)
    {
        
        $requestToken = sha1(uniqid(rand(), true));
        $requestTokenSecret = sha1(uniqid(rand(), true));
        
        $tokenTable = new Model_DbTable_Tokens();
        $tokenRow = $tokenTable->createRow();
        $tokenRow->tkn_token = $requestToken;
        $tokenRow->tkn_token_secret = $requestTokenSecret;
        $tokenRow->tkn_authenticated = 0;
        $tokenRow->tkn_state = 'REQUEST';
        $tokenRow->tkn_creation_date = time();
        $tokenRow->tkn_callback = $server->getCallbackUrl();
        $tokenRow->tkn_consumer = $server->getConsumer()->getKey();
        $tokenRow->save();
        
        $front = Zend_Controller_Front::getInstance();
        $front  ->getResponse()
                ->setHttpResponseCode(200)
                ->appendBody('oauth_token='.$requestToken.'&oauth_token_secret=' . $requestTokenSecret)
                ->sendResponse();
        exit;
    }
}
?>